from flask import Blueprint,render_template,session,request,redirect,jsonify
from model.task.ck import Cookievalue
from model import User,course,statuscourse,classInfo
from model import redisclient
from datetime import timedelta
import json
inx=Blueprint('index',__name__)
@inx.route('/')
def index():
    data=session.get('data')
 
    if data is None:
        return redirect('/login')
    sign=data.get('sign')
    if sign!=request.cookies.get('sign') or (sign==None):
        return redirect('/login')
    try:
        value=Cookievalue(username='1',role='2').cookieValueRex(data=data)
    except Exception:
        return redirect('/login')
    if value['code']!=200:
        return redirect('/login')
    if data['role']=='admin':
        return render_template('admin.html',username=data['username'])
    elif data['role']=='teacher':
        return render_template('teacher.html',username=data['username'])
    else:
        return render_template('user.html',username=data['username'])
@inx.route('/getmap')
def getmap():

    finduser=User.query.filter_by(role='user').all()
    userid=[]
    for i in finduser:
        userid.append(i.id)

    findclassname=classInfo.query.filter(classInfo.userid.in_(userid)).all()
    classnamedata=[]
    for j in findclassname:
        classnamedata.append(j.classname)
    classnamedata=list(set(classnamedata))
    findclasslist=classInfo.query.filter(classInfo.classname.in_(classnamedata),classInfo.userid==None).all()
   
    classiddata=[]
    for j in findclasslist:
        classiddata.append(j.id)
    print(classiddata)
    findcourse=course.query.filter(course.classid.in_(classiddata)).all()
    print(findcourse)
    couseiddata=[]
    corsemaxnumber=len(findcourse)*len(finduser)
    for h in findcourse:
        couseiddata.append(h.id)
   
    findsstatuscorse=statuscourse.query.filter(statuscourse.courseid.in_(couseiddata)).all()
    data=[corsemaxnumber,len(findsstatuscorse)]
 
    return jsonify(code=200,data=data)